
var hotLinkType = {
		"name" : "cgm.link.name",
		"id" : "cgm.link.id"
};

var Media = function (){
	var medias = new Object();
	return {
		createMedia : function(options){
			if(options.id && options.url){
				var fileExtension = options.url.substr(options.url.lastIndexOf("."));
				switch(fileExtension.toLowerCase()){
					case ".cgm" : 
						medias[options.id] = new cgmMedia(options);
						break;
					case ".unity3d" :
						medias[options.id] = new u3dMedia(options);
						break;
					case ".wrl":
						medias[options.id] = new wrlMedia(options);
						break;
					default:
						return null;
				}
				return medias[options.id];
			}
		},
		getMedia : function(id){
			if(id){
				if(typeof(medias[id]) === "undefined"){
					return null;
				}else{
					return medias[id];
				}
			}
		}
	};
};


/**
 * CGM
 * 1.1新增内容：增加了点击CGM图片的热点监听事件会传回两个参数，第一个参数是热点ID，第二个参数是热点名称。
 * **********************************************************************************************************
 * 1.2新增内容                                                                                                                                            *
 *		1、热点触发事件highLight的参数由原来的ID改变为name
 *		2、修改了点击CGM图片的热点监听事件会传回三个参数，第一个参数是热点ID，第二个参数是热点名称，第 *
 *			 三个参数是CGM图片的ICN编号。
 * **********************************************************************************************************
 */
var cgmMedia = function(_options) {
	var defOptions = {
		width : "100%",
		height : "100%",
		click : function(){},
		mouseover : function(){},
		mouseout : function(){},
	};
	/*<object id="ivx1" data="ex_Metafile.cgm" 
    type="image/cgm;Version=4;ProfileId=WebCGM" 
        width="480" height="360"></object>*/
	var options = $.extend(true, {}, defOptions, _options);
	/*var temp = '<object' + ' id="' + options.id + '"' + ' type="'
			+ "image/cgm;Version=4;Profiled=WebCGM" + '"' + ' data="'
			+ options.url + '"';*/
	var temp = '<object' + ' id="' + options.id + '"' + ' classid="'
	+ "CLSID:1D4A81CB-25A3-4EB7-A75E-4F8A5386D8F8" + '"' + ' data="'
	+ options.url + '"';
	if (options.width !== 0) {
		temp += ' width=' + options.width;
	}

	if (options.height !== 0) {
		temp += ' height=' + options.height;
	}
	temp += ' ></object>';
	
	var oldTargs = new Object();
	var obj = null;

	document.getElementById(options.id).outerHTML = temp;
	obj = document.getElementById(options.id);
	var intervalID = setInterval(function(){
		 try {
		      	var webcgm = obj.getWebCGMDocument();
		  		webcgm.addEventListener("click",function(event){
					var id = event.target.apsid;
					var name = event.target.getAppStructureAttr("name");
					options.click(id,name,options.id);
					removeAllHighLight();
					highLightById(id);
		  		});
		  		webcgm.addEventListener("mouseover",function(event){
					var id = event.target.apsid;
					var name = event.target.getAppStructureAttr("name");
					options.mouseover(id,name,options.id);
					//removeAllHighLight();
					//highLightById(id);
		  		});
		  		webcgm.addEventListener("mouseout",function(event){
					var id = event.target.apsid;
					var name = event.target.getAppStructureAttr("name");
					options.mouseout(id,name,options.id);
					//removeAllHighLight();
					//highLightById(id);
		  		});
		  		clearInterval(intervalID);
		    } catch (e) {
		      //console.log("Catch the exception: " + e.description);
		    }
		    
	},100);
		
	
	
	function highLightById(targId) {
		 setTimeout(function(){obj.HighlightHotspot(targId, 1, 0000999);}, 100);
		 oldTargs[targId] = targId;
	}
	function unHighLightById(targId){
		setTimeout(function(){obj.HighlightHotspot(targId, 0, 0000999);}, 100);
		delete oldTargs[targId];
	}
	function removeAllHighLight() {
		for(var i in oldTargs){
			unHighLightById(oldTargs[i]);
		}
	}
	
	function highLightByName(targName,type){
		setTimeout(function(){
			try {
		      	var webcgm = obj.getWebCGMDocument();
		      	 var pic = webcgm.firstPicture;
		      	 var nodes = pic.getAppStructuresByName(targName);
		      	 if(type == "new"){
		      		removeAllHighLight();
		      	 }
		      	for (var i = 0 ;i <= nodes.count -1;i ++ ){
		      		var node = nodes.item(i);
		      		highLightById(node.apsId);
		      	}
		    } catch (e) {
		      //console.log("Catch the exception: " + e.description);
		    }
			}, 100);
	}
	
	function unHighLightByName(targName){
		setTimeout(function(){
			try {
		      	var webcgm = obj.getWebCGMDocument();
		      	 var pic = webcgm.firstPicture;
		      	 var nodes = pic.getAppStructuresByName(targName);
		      	for (var i = 0 ;i <= nodes.count -1;i ++ ){
		      		var node = nodes.item(i);
		      		unHighLightById(node.apsId);
		      	}
		    } catch (e) {
		      //console.log("Catch the exception: " + e.description);
		    }
			}, 100);
	}
	
	return {
		getOldTargs : function() {
			return $.extend({},oldTargs);
		},
		addHighLight : function(targ,type){
			if(obj){
				if(type == hotLinkType.name){
					highLightByName(targ,"add");
				}else{
					highLightById(targ);
				}
			}
		},
		newHighLight : function(targ,type){
			if(obj){
				if(type == hotLinkType.name){
					highLightByName(targ,"new");
				}else{
					highLightById(targ);
				}
			}
		},
		removeHighLight : function(targ,type){
			if(obj){
				if(type == hotLinkType.name){
					unHighLightByName(targ);
				}else{
					unHighLightById(targ);
				}
			}
		},
		clearHighLight : function(){
			if(obj){
				removeAllHighLight();
			}
		},
		width : function(width) {
			if(width){
				$(obj).width(width);
			}
			return $(obj).width();
		},
		height : function(height) {
			if(height){
				$(obj).height(height);
			}
			return $(obj).height();
		},
		getMediaType : function(){
			return 'cgm';
		}
	};
};
/**
 * U3D控件
 */
var u3dMedia = function(_options){
	var defOptions = {
			init : {
				delay : 5000
			}
		};
		var options = $.extend(true, {}, defOptions, _options);
		var u3d = new UnityObject2(options);
		var obj = document.getElementById(options.id);
		
		u3d.initPlugin($(obj),options.url);
		if(options.init.targ){
			setTimeout(function(){u3d.getUnity().SendMessage(options.init.targ, options.init.method, options.init.param);}, options.init.delay);
		}
		return{
			getUnity : function(){
				return u3d.getUnity();
			},
			width : function(width) {
				if(width){
					$(obj).width(width);
				}
				return $(obj).width();
			},
			height : function(height) {
				if(height){
					$(obj).height(height);
				}
				return $(obj).height();
			},
			getMediaType : function(){
				return 'u3d';
			}
		};
};
/*
 * WRL
*/
var wrlMedia = function (_options){
	var defOptions = {
			width : "100%",
			height : "100%",
			onNewSubstep : function(){}
	};
	var options = $.extend(true, {}, defOptions, _options);
	var temp = '<object' + ' id="' + options.id + '"' 
	+ ' classid="'+ "CLSID:86A88967-7A20-11D2-8EDA-00600818EDB1" 
	+ '"' + ' type ="application/x-oleobject"';
	
	if (options.width !== 0) {
		temp += ' width=' + options.width;
	}
	if (options.height !== 0) {
		temp += ' height=' + options.height;
	}
	temp +=' >';
	temp += '<param name="ConsoleMode" value="1">';
	temp += '<param name="ContextMenu" value="True">';
	temp += '<param name="CpuLoading" value="80">';
	temp += '<param name="LoadDroppedScene" value="False">';
	temp += '<param name="NavigationBar" value="1">';
	temp += '<param name="RendererHints" value="5168">';
	temp += '<param name="RendererName" value="DirectX Renderer">';
	temp += '<param name="RendererOptimization" value="0">';
	temp += '<param name="ShowLogo" value="False">';
	temp += '<param name="Skin" value="{46BB95BF-8EB4-481A-A1EF-50D43FC32B9D};">';
	temp += '<param name="TravelSpeed" value="1">';
	temp += '<param name="viewpoint_transition_mode" value="0">';
	temp += '<param name="WaitForAllResources" value="True">';
	temp += '<param name="BackColor" value="&hC08080">';
	temp += '</object>';
	
	document.getElementById(options.id).outerHTML = temp ;
	var obj = document.getElementById(options.id);
	var api = new SimulationAPI(obj);
	api.load(options.url);
	api.on_simulation_load = _on_simulation_loaded;
	api.set_autostop_mode(1);
	if(jQuery.isFunction(options.onNewSubstep)){
		api.on_start_new_substep = options.onNewSubstep;
	}
	
	function _on_simulation_loaded(flag){
		  scene_is_loaded = flag;
		  if(flag) {
		    api.set_speed_ratio(1);
		  }
		  else {
		    alert("3D Simulation Not Loaded");
		  }
		}
	return {
		play : function(id){
			if(id){
				api.vcr_goto(id);
			}
			api.vcr_play();
		},
		getAPI : function(){
			return api;
		},
		width : function(width) {
			if(width){
				$(obj).width(width);
			}
			return $(obj).width();
		},
		height : function(height) {
			if(height){
				$(obj).height(height);
			}
			return $(obj).height();
		},
		getMediaType : function(){
			return 'wrl';
		}
	};
};

